﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using Yang.DataBase;

public partial class WebServices_getStatInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        string resultJson = "{ \"Details\":[{";

        string machineCode = Request.QueryString[0].ToString();



        string strDate = DateTime.Now.Date.ToString();//记录当前日期
        int nPoints = 0;//统计工作点总数
        //string[] workCodeArray = new string[1]; 
        //int[] workPointArray = new int[1];
        //double[] workAreaArray = new double[1];

        try
        {

            //查询作业类型和各作业类型的点数
            string strSql = "select  DISTINCT WorkCode, WorkName from GpsInfo where LocationTime > '" + strDate + "' and DeviceCode = '" + machineCode + "' and WorkCode <> 0";
            DataSet ds = Database.Query(strSql);
            int nWorkTypes = ds.Tables[0].Rows.Count;
            if (nWorkTypes == 0)
            {
                resultJson = "{\"Details\":\"Error\"}";
                Response.Write(resultJson);
                return;
            }
            AgriMachineWork[] workArray = new AgriMachineWork[nWorkTypes];




            //if (ds.Tables[0].Rows.Count > 0)
            //{
                //int nWorkTypes = ds.Tables[0].Rows.Count;
                //workCodeArray = new string[nWorkTypes];
                //workPointArray = new int[nWorkTypes];
                //workAreaArray = new double[nWorkTypes];
            for (int i = 0; i < nWorkTypes; i++)
            {
                workArray[i] = new AgriMachineWork();
                workArray[i].WorkCode = int.Parse(ds.Tables[0].Rows[i][0].ToString());
                workArray[i].WorkName = ds.Tables[0].Rows[i][1].ToString();
                //workArray[i].WorkArea = 0;
                //workArray[i].WorkAmount = 0;
                //workArray[i].WorkMoney = 0;

                //workCodeArray[i] = ds.Tables[0].Rows[i][0].ToString();
                //workPointArray[i] = int.Parse(ds.Tables[0].Rows[i][1].ToString());
                //workAreaArray[i] = 0;
                //nPoints += workPointArray[i];
            }
            //}
            //else
            //{
            //    Log.WriteInfo("当前农机今日无作业");
            //    resultJson = "{\"Details\":\"Error\"}";
            //}



            strSql = "select Latitude,Longitude,WorkStatus,WorkWidth,WorkCode,WorkName from GpsInfo where LocationTime > '" + strDate + "' and DeviceCode = '" + machineCode + "' order by LocationTime DESC";
            ds.Clear();
            ds = Database.Query(strSql);
            double x1, x2, y1, y2;
            //double GpsRouteLength = 0.0;
            double WorkWidth = 0.0; //作业幅宽
            double WorkArea = 0.0;// 作业面积
            //double WorkMoney = 0.0;//作业价值
            nPoints = ds.Tables[0].Rows.Count;

            if (nPoints > 0)
            {
                //if (ds.Tables[0].Rows.Count == nPoints)
                //{
                for (int i = 1; i < nPoints; i++)
                {
                    if (int.Parse(ds.Tables[0].Rows[i][2].ToString()) == 1 && ds.Tables[0].Rows[i][4].ToString() == ds.Tables[0].Rows[i - 1][4].ToString())
                    {
                        y1 = double.Parse(ds.Tables[0].Rows[i][0].ToString());
                        x1 = double.Parse(ds.Tables[0].Rows[i][1].ToString());
                        y2 = double.Parse(ds.Tables[0].Rows[i - 1][0].ToString());
                        x2 = double.Parse(ds.Tables[0].Rows[i - 1][1].ToString());
                        WorkWidth = (double.Parse(ds.Tables[0].Rows[i - 1][3].ToString())+ double.Parse(ds.Tables[0].Rows[i][3].ToString())) / 2;
                        //(double.Parse(ds.Tables[0].Rows[i + 1][3].ToString()) + double.Parse(ds.Tables[0].Rows[i][3].ToString())) / 2;
                        //GpsRouteLength += GetDistance(y1, x1, y2, x2);// *WorkWidth;
                        WorkArea = GetDistance(y1, x1, y2, x2) * WorkWidth;
                        for (int j = 0; j < nWorkTypes; j++)
                        {
                            if (workArray[j].WorkCode == int.Parse(ds.Tables[0].Rows[i][4].ToString()))
                            {
                                workArray[j].WorkArea += WorkArea;
                            }
                        }
                    }
                }
                //}
            }
            else
            {
                Log.WriteInfo("农机未开启");
                resultJson = "{\"Details\":\"Error\"}";
            }

            for (int i = 0; i < nWorkTypes; i++)
            {
                workArray[i].setWorkPriceAndRatio(workArray[i].WorkCode);

                resultJson += "\"WorkName\":\"" + workArray[i].WorkName + "\",";
                resultJson += "\"WorkAmount\":\"" + workArray[i].WorkAmount + "\",";
                resultJson += "\"WorkMoney\":\"" + workArray[i].WorkMoney + "\"},";
            }

            if (resultJson.IndexOf("Error") < 0)
            {
                resultJson = resultJson.Substring(0, resultJson.Length - 1);
                resultJson += "]}";
            }
        }
        catch (Exception ex)
        {
            Log.WriteException(DateTime.Now.ToString() + ":" + ex.Message.ToString() + "\n");
            resultJson = "{\"Details\":\"Error\"}";
        }
        Response.Write(resultJson);


    }



    private const double EARTH_RADIUS = 6378137.0;//地球半径

    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
         Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.Round(s * 10000) / 10000;
        return s;
    }
}
